import React, { Component } from 'react'
import { Route, Redirect } from 'react-router-dom'
import { hasToken } from 'utils/storage'
export default class PrivateRoute extends Component {
  render() {
    const { component: Component, path, ...res } = this.props
    return (
      <Route {...res} path={path} render={(props) => {
        if (hasToken()) {
          return <Component {...props}></Component>
        } else {
          return <Redirect
            to={{
              pathname: '/login',
              search: '?from=' + props.location.pathname,
              state: {
                from: props.location.pathname
              }
            }}></Redirect>
        }
      }}></Route>
    )
  }
}
